package com.placedial;

import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;

public class IDE extends JFrame{

	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField_input = null;
	private JTextField jTextField_output = null;
	private JScrollPane jScrollPane = null;
	private JTextArea jTextArea = null;
	private JButton findCovers = null;

	/**
	 * This method initializes 
	 * 
	 */
	public IDE() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setSize(new Dimension(659, 375));
        this.setContentPane(getJPanel());
			
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(90, 272, 67, 16));
			jLabel1.setText("output file");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(90, 225, 66, 16));
			jLabel.setText("Input file");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(jLabel, null);
			jPanel.add(jLabel1, null);
			jPanel.add(getJTextField_input(), null);
			jPanel.add(getJTextField_output(), null);
			jPanel.add(getJScrollPane(), null);
			jPanel.add(getFindCovers(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTextField_input	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField_input() {
		if (jTextField_input == null) {
			jTextField_input = new JTextField();
			jTextField_input.setBounds(new Rectangle(179, 222, 125, 20));
		}
		return jTextField_input;
	}

	/**
	 * This method initializes jTextField_output	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField_output() {
		if (jTextField_output == null) {
			jTextField_output = new JTextField();
			jTextField_output.setBounds(new Rectangle(181, 269, 124, 20));
		}
		return jTextField_output;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(88, 30, 269, 181));
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
		}
		return jTextArea;
	}

	/**
	 * This method initializes findCovers	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getFindCovers() {
		if (findCovers == null) {
			findCovers = new JButton();
			findCovers.setBounds(new Rectangle(94, 309, 134, 20));
			findCovers.setText("Find Cover Set");
			findCovers.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e){
					String inputpath="";
					String outputpath="";
					if(jTextField_input.getText()!=null&&jTextField_output.getText()!=null){
						   inputpath=jTextField_input.getText();
						   outputpath=jTextField_output.getText();
					}
					try{
						ParseInput parse=new ParseInput();
						
						
						parse.convertToGraph(inputpath);
						if(!parse.getError().equals("")){
							//System.out.println(parse.getError());
							jTextArea.setText(parse.getError());
						}
						else{
						
						 
						 //System.out.println("apply kernalization: ");
						 Set cover=new HashSet();
						 GraphP gp=new GraphP(parse.getVSet(), cover, parse.getK(), parse.getSize(), parse.getGraph());
						
						 KB kb=new KB();
						 kb.boundSearchTree(gp);
						 
						 if(kb.getReturnGp()!=null){
						   Set coverset=kb.getReturnGp().getCoverSet();
						   int k=coverset.size();
						   String message="Vertex cover size: "+k+parse.getLineseperator()+"vertex cover set:"+ 
						                   coverset.toString()+parse.getLineseperator();
						   jTextArea.setText(message);
						   try {
							      PrintStream out = new PrintStream(new FileOutputStream(
							          new File(outputpath)));
							      
							        out.print(message);

							      out.close();

							    } catch (Exception exception) {
							    	JOptionPane.showMessageDialog(null,exception.getMessage());
							    }
						   
						 
						 }else{
							 jTextArea.setText("NO");
						 }
						 
						 
						
							 
						
						 
						 
						}
						
						
					}catch(Exception ex){
						JOptionPane.showMessageDialog(null,ex.getMessage());
					}
					
					
					
					
				}
				});
		}
		return findCovers;
	}
	
	
	

}  //  @jve:decl-index=0:visual-constraint="10,10"
